package com.smartao.core.orm.mybatis;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.springframework.orm.jpa.vendor.Database;

import com.smartao.core.orm.DatabaseDetector;

/**
 * 获取数据库类型实现多数据库支持
 * @author Ricky
 *
 */
public class MybatisVendorDatabaseIdProvider implements DatabaseIdProvider {

	@Override
	public void setProperties(Properties p) {}

	/**
	 * 返回 {@link DatabaseDetector#getDatabase()}中的 Database<br>
	 * eg. DERBY, MYSQL ,H2 ,ORACLE
	 */
	@Override
	public String getDatabaseId(DataSource dataSource) throws SQLException {
		DatabaseDetector databaseDetector = new DatabaseDetector();
		databaseDetector.setDataSource(dataSource);
		Database database = databaseDetector.getDatabase();
		return database.name();
	}

}
